﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace Example2.Model
{
    class Level
    {
        
        public const int LEVEL_WIDTH = 10;
        public const int LEVEL_HEIGHT = 10;
        public const int TILE_WIDTH = 1;
        public const int TILE_HEIGHT = 1;
        

        public bool[][] m_tiles = new bool[LEVEL_WIDTH][];

        public Level()
        {
            for(int x = 0; x < LEVEL_WIDTH; ++x) {
                m_tiles[x] = new bool[LEVEL_HEIGHT];
                for (int y = 0; y < LEVEL_HEIGHT; ++y)
                {
                    m_tiles[x][y] = false;
                }

            }
        }

        internal void ToggleTile(Microsoft.Xna.Framework.Vector2 position)
        {
            int x = (int)position.X;
            int y = (int)position.Y;

            if (x < 0 || y < 0 || x >= LEVEL_WIDTH || y >= LEVEL_HEIGHT)
            {
                return;
            }

            m_tiles[x][y] = !m_tiles[x][y];

        }

        internal bool doBallAndTilesColliding(Microsoft.Xna.Framework.Vector2 a_centerPosition, float a_radius, out Vector2 a_normal)
        {
            for (int x = 0; x < LEVEL_WIDTH; ++x)
            {
                for (int y = 0; y < LEVEL_HEIGHT; ++y)
                {
                    if (m_tiles[x][y]) {
                        if (tileCollide(x, y, a_centerPosition, a_radius, out a_normal))
                        {
                            m_tiles[x][y] = false;
                            return true;
                        }
                    }
                }
            }
            a_normal.X = 0;
            a_normal.Y = 0;
            return false;
        }

        private bool tileCollide(float a_x, float a_y, Vector2 a_centerPosition, float a_radius, out Vector2 a_normal)
        {

            FloatRectangle rightBox = FloatRectangle.createFromTopLeft(new Vector2(a_x+1.0f, a_y), new Vector2(a_radius, 1));
            FloatRectangle leftBox = FloatRectangle.createFromTopLeft(new Vector2(a_x - a_radius, a_y), new Vector2(a_radius, 1));
            FloatRectangle topBox = FloatRectangle.createFromTopLeft(new Vector2(a_x, a_y - a_radius), new Vector2(1, a_radius));
            FloatRectangle bottomBox = FloatRectangle.createFromTopLeft(new Vector2(a_x, a_y + 1.0f), new Vector2(1, a_radius));

            
            
            Vector2[] corners = { new Vector2(a_x, a_y), 
                                  new Vector2(a_x + 1.0f, a_y), 
                                  new Vector2(a_x + 1.0f, a_y + 1.0f), 
                                  new Vector2(a_x, a_y + 1.0f) };

            if (rightBox.isIntersecting(a_centerPosition))
            {
                a_normal.X = 1;
                a_normal.Y = 0;
                return true;
            }

            if (leftBox.isIntersecting(a_centerPosition))
            {
                a_normal.X = -1;
                a_normal.Y = 0;
                return true;
            }

            if (topBox.isIntersecting(a_centerPosition))
            {
                a_normal.X = 0;
                a_normal.Y = -1;
                return true;
            }

            if (bottomBox.isIntersecting(a_centerPosition))
            {
                a_normal.X = 0;
                a_normal.Y = 1;
                return true;
            }

            
            for (int i = 0; i < 4; i++)
            {
                Vector2 dir = a_centerPosition - corners[i];
                float distance = dir.Length();
                if (distance < a_radius)
                {
                    dir.Normalize();
                    a_normal = dir;
                    return true;
                }
            }

            a_normal.X = 0;
            a_normal.Y = 0;
            return false;
        }
    }
}
